/*******************************************************************************
  MPLAB Harmony Application Header File

  Company:
    Microchip Technology Inc.

  File Name:
    app.h

  Summary:
    This header file provides prototypes and definitions for the application.

  Description:
    This header file provides function prototypes and data type definitions for
    the application.  Some of these are required by the system (such as the
    "APP_Initialize" and "APP_Tasks" prototypes) and some of them are only used
    internally by the application (such as the "APP_STATES" definition).  Both
    are defined here for convenience.
*******************************************************************************/

// DOM-IGNORE-BEGIN
/*******************************************************************************
* Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
// DOM-IGNORE-END

#ifndef _APP_H
#define _APP_H

// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "configuration.h"
#include "system/fs/sys_fs.h"

// DOM-IGNORE-BEGIN
#ifdef __cplusplus  // Provide C++ Compatibility

extern "C" {

#endif
// DOM-IGNORE-END

// *****************************************************************************
// *****************************************************************************
// Section: Type Definitions
// *****************************************************************************
// *****************************************************************************
#define APP_MAX_MEM_PAGE_SIZE                           (8192UL)
#define APP_PROTOCOL_HEADER_MAX_SIZE                    9

// *****************************************************************************
/* Application states

  Summary:
    Application states enumeration

  Description:
    This enumeration defines the valid application states.  These states
    determine the behavior of the application at various times.
*/

typedef enum
{
    /* Application's state machine's initial state. */
    APP_INIT = 0,
    APP_DISK_MOUNT_WAIT,
    APP_CURRENT_DRIVE_SET,
    APP_FILE_OPEN,
    APP_WAIT_SWITCH_PRESS,
    APP_LOAD_I2C_SLAVE_DATA,
    APP_SEND_READ_VERSION_COMMAND,
    APP_WAIT_READ_VERSION_COMMAND_TRANSFER_COMPLETE,
    APP_SEND_UNLOCK_COMMAND,
    APP_WAIT_UNLOCK_COMMAND_TRANSFER_COMPLETE,
    APP_SEND_ERASE_COMMAND,
    APP_WAIT_ERASE_COMMAND_TRANSFER_COMPLETE,
    APP_SEND_WRITE_COMMAND,
    APP_WAIT_WRITE_COMMAND_TRANSFER_COMPLETE,
    APP_SEND_VERIFY_COMMAND,
    APP_WAIT_VERIFY_COMMAND_TRANSFER_COMPLETE,
    APP_READ_STATUS,
    APP_READ_STATUS_COMMAND_TRANSFER_COMPLETE,
    APP_OPEN_DEVCFG_FILE,
    APP_READ_DEVCFG_DATA,
    APP_SEND_DEVCFG_ERASE_COMMAND,
    APP_WAIT_DEVCFG_ERASE_COMMAND_TRANSFER_COMPLETE,
    APP_SEND_DEVCFG_WRITE_COMMAND,
    APP_WAIT_DEVCFG_WRITE_COMMAND_TRANSFER_COMPLETE,
    APP_SEND_RESET_COMMAND,
    APP_SEND_BANK_SWAP_RESET_COMMAND,
    APP_WAIT_RESET_COMMAND_TRANSFER_COMPLETE,
    APP_SUCCESSFUL,
    APP_ERROR,
    APP_IDLE,
    /* TODO: Define states used by the application state machine. */

} APP_STATES;

typedef enum
{
    APP_TRANSFER_STATUS_IN_PROGRESS,
    APP_TRANSFER_STATUS_SUCCESS,
    APP_TRANSFER_STATUS_ERROR,
    APP_TRANSFER_STATUS_IDLE,

} APP_TRANSFER_STATUS;

typedef enum
{
    APP_BL_COMMAND_UNLOCK = 0xA0,
    APP_BL_COMMAND_ERASE = 0xA1,
    APP_BL_COMMAND_PROGRAM = 0xA2,
    APP_BL_COMMAND_VERIFY = 0xA3,
    APP_BL_COMMAND_RESET = 0xA4,
    APP_BL_COMMAND_READ_STATUS = 0xA5,
    APP_BL_COMMAND_BKSWAP_RESET = 0xA6,
    APP_BL_COMMAND_DEVCFG_PROGRAM = 0xA7,
    APP_BL_COMMAND_READ_VERSION = 0xA8,
}APP_BL_COMMAND;

typedef struct
{
    uint16_t                i2cSlaveAddr;
    uint32_t                erasePageSize;
    uint32_t                programPageSize;
    uint32_t                appStartAddr;
    char*                   filename;
    bool                    devCfgProgram;
    bool                    bankSwap;
    char*                   devCfgFileName;
    uint32_t                devCfgAddr;
    uint32_t                devCfgData[APP_ERASE_PAGE_SIZE/sizeof(uint32_t)];

} APP_FIRMWARE_UPDATE_INFO;

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    Application strings and buffers are be defined outside this structure.
 */

typedef struct
{
    APP_STATES                                  state;
    APP_STATES                                  nextState;
    uint8_t                                     i2cSlaveIndex;
    uint16_t                                    i2cSlaveAddr;
    uint8_t                                     percentageDone;
    uint32_t                                    erasePageSize;
    uint32_t                                    programPageSize;
    uint32_t                                    appStartAddr;
    /* SDCard related variables */
    SYS_FS_HANDLE                               fileHandle;
    uint32_t                                    fileSize;
    volatile bool                               isSDCardMount;
    /* I2C Bootloader related variables */
    volatile APP_TRANSFER_STATUS                trasnferStatus;
    uint32_t                                    appImageSize;
    uint32_t                                    imageSizeMultipleOfPage;
    uint32_t                                    nBytesWritten;
    uint32_t                                    nBytesWrittenInErasedPage;
    uint32_t                                    crcVal;
    uint8_t                                     status;
    uint16_t                                    btlVersion;
    uint8_t                                     wrBuffer[APP_MAX_MEM_PAGE_SIZE + APP_PROTOCOL_HEADER_MAX_SIZE];

} APP_DATA;

// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Routines
// *****************************************************************************
// *****************************************************************************
/* These routines are called by drivers when certain events occur.
*/

// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

/*******************************************************************************
  Function:
    void APP_Initialize ( void )

  Summary:
     MPLAB Harmony application initialization routine.

  Description:
    This function initializes the Harmony application.  It places the
    application in its initial state and prepares it to run so that its
    APP_Tasks function can be called.

  Precondition:
    All other system initialization routines should be called before calling
    this routine (in "SYS_Initialize").

  Parameters:
    None.

  Returns:
    None.

  Example:
    <code>
    APP_Initialize();
    </code>

  Remarks:
    This routine must be called from the SYS_Initialize function.
*/

void APP_Initialize ( void );


/*******************************************************************************
  Function:
    void APP_Tasks ( void )

  Summary:
    MPLAB Harmony Demo application tasks function

  Description:
    This routine is the Harmony Demo application's tasks function.  It
    defines the application's state machine and core logic.

  Precondition:
    The system and application initialization ("SYS_Initialize") should be
    called before calling this.

  Parameters:
    None.

  Returns:
    None.

  Example:
    <code>
    APP_Tasks();
    </code>

  Remarks:
    This routine must be called from SYS_Tasks() routine.
 */

void APP_Tasks( void );



//DOM-IGNORE-BEGIN
#ifdef __cplusplus
}
#endif
//DOM-IGNORE-END

#endif /* _APP_H */

/*******************************************************************************
 End of File
 */

